module.exports = class extends think.Model {

    async adminData(id) {
        let result = await this.query(`SELECT
        t.*,
        g.group_name,
        s.system_name,
        s.system_url 
    FROM
        tab_api AS t
        JOIN tab_group AS g ON g.group_id = t.group_id
        JOIN tab_system AS s ON s.system_id = t.system_id 
    WHERE
        t.system_id = '${id}'`)
        let project = await this.model('system').where({
            system_id: id
        }).find();
        return {
            result: result,
            project: project
        };
    }

    //查询接口项目
    async indexQueryProject() {
        let result = await this.query(`SELECT
            tsys.*,
            temp.count 
        FROM
            tab_system tsys
            LEFT JOIN (
        SELECT
            count( api_id ) AS count,
            system_id AS id 
        FROM
            tab_api 
        GROUP BY
            system_id 
            ) AS temp ON temp.id = tsys.system_id 
        ORDER BY
            tsys.system_time DESC`)
        return result;
    }
    //添加项目接口
    async adminInsertProject(params) {
        console.log(params);
        let result = await this.model('system').add(params);
        return result;
    }
    // 删除项目
    async adminDelete(id) {
        let result = await this.query(`DELETE 
     t1.* ,
     t2.*
    FROM
        tab_system AS t1
    LEFT JOIN tab_api AS t2 ON t1.system_id = t2.system_id
    WHERE
        t1.system_id  = '${id}'`)
        return result;
    }
    //编辑项目
    async adminEdit(parmas) {
        let result = await this.model('system').where({
            system_id: parmas.system_id
        }).update({
            system_name: parmas.system_name,
            system_version: parmas.system_version,
            system_desc: parmas.system_desc,
            system_url: parmas.system_url,
            system_time: ['exp', 'CURRENT_TIMESTAMP()']
        })
        return result;
    }

    // 查找对于的接口
    async indexFindById(id) {
        let result = await this.model('api').where(id).find();
        return result;
    }

    // 查找接口分组
    async indexFindGroup(id) {
        let result = await this.model('group').where({
            system_id: id
        }).select();
        return result;
    }

//添加接口
    async addApiMethod(parmas) {

        let result = await this.model('api').add(parmas);
        return result;
    }
// 删除接口
    async indexDeleteApi(id) {

        let result = await this.model('api').where(id).delete();
        return result;
    }
    // 修改接口
    async indexUpdateApi(parmas) {

        let result = await this.model('api').where({
            api_id: parmas.api_id
        }).update({
            "api_name": parmas.api_name,
            "api_path": parmas.api_path,
            "api_params": parmas.api_params,
            "api_desc": parmas.api_desc,
            "group_id": parmas.group_id,
            "api_method": parmas.api_method,
        });
        return result;
    }

    //添加分类
    async addGroupMethod(params) {
        let result = await this.model('group').add(params);
        return result;
    }
    //删除分类
    async deleteGroupMethod(params) {
        let result = await this.model('group').where({
            group_id: params
        }).delete();
        return result;
    }
};